home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1998 April: Mac OS SDK / Dev.CD Apr 98 SDK1.toast / Development Kits (Disc 1) / Macintosh Drag and Drop / Demo Applications / Dragster / DragText Sources / menus.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-01-03  |  10.2 KB  |  455 lines  |  [TEXT/KAHL]

  1. /*
  2.  *
  3.  *        menus.c
  4.  *
  5.  *        Menu handling routines and utilities.
  6.  *        
  7.  *
  8.  *        Author:        Rob Johnston
  9.  *        Date:        Wednesday, February 26, 1992
  10.  *
  11.  *        12/30/94    JML        Code now compiles with Universal interfaces.
  12.  *
  13.  *        Copyright © 1992 Apple Computer, Inc.
  14.  *
  15.  */
  16.  
  17. #include <Desk.h>
  18. #include <Devices.h>
  19. #include <ToolUtils.h>
  20. #include <Fonts.h>
  21. #include <Scrap.h>
  22. #include <Dialogs.h>
  23. #include <Drag.h>
  24. #include "globals.h"
  25. #include "prototypes.h"
  26. #include "DTResources.h"
  27.  
  28.  
  29. /*
  30.  *    Given a range of menu items and a menu, FindCheckItem returns the
  31.  *    item number of the first checked item. If no items in the range
  32.  *    are checked, FindCheckItem returns 0 (zero).
  33.  */
  34.  
  35. short FindCheckItem(MenuHandle theMenu, short rangeStart, short rangeEnd)
  36.  
  37. {    short        theMark;
  38.  
  39.     while (rangeStart <= rangeEnd) {
  40.         GetItemMark(theMenu, rangeStart, &theMark);
  41.         if (theMark == checkMark)
  42.             return(rangeStart);
  43.         rangeStart++;
  44.     }
  45.  
  46.     return(0);
  47. }
  48.  
  49.  
  50. /*
  51.  *    Check1Item checks a single menu item in a range of menu items. theItem is
  52.  *    the item number to check. rangeStart and rangeEnd denote a range of menu
  53.  *    items that theItem belongs to. Check1Item checks theItem and removes
  54.  *    any checks from the other items in the given range. Check1Item returns
  55.  *    the item number of the checked item if a newly checked item is selected.
  56.  *    If theItem was already checked or theItem was outside of the given range,
  57.  *    Check1Item returns 0 (zero).
  58.  */
  59.  
  60. short Check1Item(MenuHandle theMenu, short theItem, short rangeStart, short rangeEnd)
  61.  
  62. {    short        theMark;
  63.  
  64.     if ((theItem < rangeStart) || (theItem > rangeEnd))
  65.         return(0);
  66.  
  67.     while (rangeStart <= rangeEnd) {
  68.         if (rangeStart != theItem)
  69.             CheckItem(theMenu, rangeStart, false);
  70.         rangeStart++;
  71.     }
  72.  
  73.     GetItemMark(theMenu, theItem, &theMark);
  74.  
  75.     if (theMark != checkMark) {
  76.         CheckItem(theMenu, theItem, true);
  77.         return(theItem);
  78.     } else {
  79.         return(0);
  80.     }
  81. }
  82.  
  83.  
  84. /*
  85.  *    ItemCheck returns true if the given menu item is checked, false otherwise.
  86.  */
  87.  
  88. short ItemCheck(MenuHandle theMenu, short theItem)
  89.  
  90. {    short        theMark;
  91.  
  92.     GetItemMark(theMenu, theItem, &theMark);
  93.     return(theMark == checkMark);
  94.  
  95. }
  96.  
  97.  
  98. /*
  99.  *    ToggleCheck toggles the check mark on the given menu item. ToggleCheck
  100.  *    returns true if the item becomes checked and false if the item becomes
  101.  *    unchecked.
  102.  */
  103.  
  104. short ToggleCheck(MenuHandle theMenu, short theItem)
  105.  
  106. {    short        theMark;
  107.  
  108.     GetItemMark(theMenu, theItem, &theMark);
  109.     CheckItem(theMenu, theItem, theMark = (theMark != checkMark));
  110.     return(theMark);
  111. }
  112.  
  113.  
  114. /*
  115.  *    SetItemEnable enables or disables a menu item depending on the value
  116.  *    of the enable parameter.
  117.  */
  118.  
  119. void SetItemEnable(MenuHandle theMenu, short theItem, short enable)
  120.  
  121. {
  122.     if (enable) {
  123.         EnableItem(theMenu, theItem);
  124.     } else {
  125.         DisableItem(theMenu, theItem);
  126.     }
  127. }
  128.  
  129.  
  130. /*
  131.  *    Given a string and a menu, ItemStringToItem will return the item number
  132.  *    of the last item that matches the string.
  133.  */
  134.  
  135. short ItemStringToItem(Str255 theItemString, MenuHandle theMenu)
  136.  
  137. {    short        index;
  138.     Str255        theString;
  139.  
  140.     index = CountMItems(theMenu);
  141.     while (index) {
  142.         GetItem(theMenu, index, theString);
  143.         if (PStrCmp(&theString, theItemString))
  144.             return(index);
  145.         index--;
  146.     }
  147.     return(0);
  148. }
  149.  
  150.  
  151. short FontToItem(short fontFamilyNumber)
  152.  
  153. {    Str255        fontName;
  154.  
  155.     GetFontName(fontFamilyNumber, fontName);
  156.     return(ItemStringToItem(fontName, GetMHandle(idFontMenu)));
  157. }
  158.  
  159.  
  160. short SizeToItem(short theSize)
  161.  
  162. {    Str255        theString;
  163.  
  164.     if (theSize == 9)
  165.         return(1);
  166.     NumToString((long) theSize, theString);
  167.     return(ItemStringToItem(theString, GetMHandle(idSizeMenu)));    
  168. }
  169.  
  170.  
  171. short ItemToSize(short theItem)
  172.  
  173. {    long        theSize;
  174.     Str255        theString;
  175.  
  176.     if (theItem == 1)
  177.         return(9);
  178.     GetItem(GetMHandle(idSizeMenu), theItem, theString);
  179.     StringToNum(theString, &theSize);
  180.     return((short) theSize);
  181. }
  182.  
  183.  
  184. /*
  185.  *    PrepareMenus is called before the user pulls down a menu from the menu
  186.  *    bar or types a command key equivalent. PrepareMenus enables and disables
  187.  *    menu items within the current program context. Any other menu related
  188.  *    setup may be performed here.
  189.  */
  190.  
  191. void PrepareMenus()
  192.  
  193. {    MenuHandle        theMenu;
  194.     Document        *theDocument;
  195.     short            teSelection, theMode, index;
  196.     TextStyle        theStyle;
  197.     Str255            theStr;
  198.  
  199.     theDocument = IsDocumentWindow(FrontWindow());
  200.     teSelection = (theDocument) && 
  201.                   ((**(theDocument->theTE)).selStart !=
  202.                    (**(theDocument->theTE)).selEnd);
  203.  
  204.     theMenu = GetMHandle(idFileMenu);
  205.  
  206.     SetItemEnable(theMenu, NewItem,   gDocumentCount < MaxDocumentCount);
  207.     SetItemEnable(theMenu, OpenItem,  gDocumentCount < MaxDocumentCount);
  208.  
  209.     SetItemEnable(theMenu, CloseItem, theDocument != 0L);
  210.     SetItemEnable(theMenu, SaveItem, (theDocument) && (theDocument->dirty));
  211.     SetItemEnable(theMenu, SaveAsItem, theDocument != 0L);
  212.     SetItemEnable(theMenu, RevertItem, (theDocument) &&
  213.                                        (theDocument->dirty) &&
  214.                                        (theDocument->fRefNum));
  215.  
  216.     SetItemEnable(theMenu, PageSetupItem, false);
  217.     SetItemEnable(theMenu, PrintItem, false);
  218.  
  219.     theMenu = GetMHandle(idEditMenu);
  220.  
  221.     GetIndString(theStr, MenuStringsID, gCanUndoDrag);
  222.     SetItem(theMenu, iUndo, theStr);
  223.     SetItemEnable(theMenu, iUndo, gCanUndoDrag != slCantUndo);
  224.  
  225.     SetItemEnable(theMenu, iCut, teSelection);
  226.     SetItemEnable(theMenu, iCopy, teSelection);
  227.     SetItemEnable(theMenu, iPaste, theDocument != 0L);
  228.     SetItemEnable(theMenu, iClear, teSelection);
  229.     SetItemEnable(theMenu, iSelectAll, theDocument != 0L);
  230.     SetItemEnable(theMenu, iShowClipboard, false);
  231.  
  232.     theMenu = GetMHandle(idFontMenu);
  233.     SetItemEnable(theMenu, 0, theDocument != 0L);
  234.     if (gFontItem)
  235.         CheckItem(theMenu, gFontItem, false);
  236.     gFontItem = 0;
  237.  
  238.     theMenu = GetMHandle(idSizeMenu);
  239.     SetItemEnable(theMenu, 0, theDocument != 0L);
  240.     if (gSizeItem)
  241.         CheckItem(theMenu, gSizeItem, false);
  242.     gSizeItem = 0;
  243.     index = CountMItems(theMenu);
  244.     while (index) {
  245.         SetItemStyle(theMenu, index, normal);
  246.         index--;
  247.     }
  248.  
  249.     theMenu = GetMHandle(idStyleMenu);
  250.     SetItemEnable(theMenu, 0, theDocument != 0L);
  251.     index = CountMItems(theMenu);
  252.     while (index) {
  253.         CheckItem(theMenu, index--, false);
  254.     }
  255.  
  256.     if (theDocument) {
  257.         theMode = doFont + doFace + doSize;
  258.         TEContinuousStyle(&theMode, &theStyle, theDocument->theTE);
  259.  
  260.         if (theMode & doFont) {
  261.             CheckItem(GetMHandle(idFontMenu),
  262.                       gFontItem = FontToItem(theStyle.tsFont), true);
  263.  
  264.             theMenu = GetMHandle(idSizeMenu);
  265.             index = CountMItems(theMenu);
  266.             while (index) {
  267.                 if (RealFont(theStyle.tsFont, ItemToSize(index)))
  268.                     SetItemStyle(theMenu, index, outline);
  269.                 index--;
  270.             }
  271.         }
  272.  
  273.         theMenu = GetMHandle(idSizeMenu);
  274.         if (theMode & doSize) {
  275.             CheckItem(theMenu, gSizeItem = SizeToItem(theStyle.tsSize), true);
  276.         }
  277.  
  278.         theMenu = GetMHandle(idStyleMenu);
  279.         if (theMode & doFace) {
  280.             if (! theStyle.tsFace)
  281.                 CheckItem(theMenu, iPlainText, true);
  282.             if (theStyle.tsFace & bold)
  283.                 CheckItem(theMenu, iBold, true);
  284.             if (theStyle.tsFace & italic)
  285.                 CheckItem(theMenu, iItalic, true);
  286.             if (theStyle.tsFace & underline)
  287.                 CheckItem(theMenu, iUnderline, true);
  288.             if (theStyle.tsFace & outline)
  289.                 CheckItem(theMenu, iOutline, true);
  290.             if (theStyle.tsFace & shadow)
  291.                 CheckItem(theMenu, iShadow, true);
  292.             if (theStyle.tsFace & extend)
  293.                 CheckItem(theMenu, iExtended, true);
  294.             if (theStyle.tsFace & condense)
  295.                 CheckItem(theMenu, iCondensed, true);
  296.         }
  297.     }
  298. }
  299.  
  300.  
  301. short DoSpecialPaste(Document *theDocument)
  302.  
  303. {    long        size, offset;
  304.     Handle        theData;
  305.  
  306.     size = GetScrap(0L, 'UPRC', &offset);
  307.  
  308.     if (size <= 0)
  309.         return(0);
  310.  
  311.     theData = NewHandle(size);
  312.     GetScrap(theData, 'UPRC', &offset);
  313.  
  314.     HLock(theData);
  315.     PutScrap(size, 'test', *theData);
  316.  
  317.     HUnlock(theData);
  318.     DisposeHandle(theData);
  319. }
  320.  
  321.  
  322.  
  323. /*
  324.  *    DoMenuCommand dispatches menu command routines from a menu select parameter.
  325.  *    This function is called when the user selects a menu item with the mouse
  326.  *    or types a command key equivalent.
  327.  */
  328.  
  329. void DoMenuCommand(long select)
  330.  
  331. {    short        theMenuID, theItem, theFontNumber, result;
  332.     MenuHandle    theMenu;
  333.     Str255        theName;
  334.     WindowPtr    theWindow;
  335.     Document    *theDocument;
  336.     Point        where;
  337.     RGBColor    outColor;
  338.  
  339.     theDocument = IsDocumentWindow(theWindow = FrontWindow());
  340.  
  341.     theItem   = LoWord(select);
  342.     theMenuID = HiWord(select);
  343.     theMenu   = GetMHandle(theMenuID);
  344.     switch(theMenuID) {
  345.         case idAppleMenu:
  346.             switch(theItem) {
  347.                 case AboutItem:
  348.                     result = Alert(256, 0L);
  349.                     break;
  350.                 default:
  351.                     GetItem(GetMHandle(idAppleMenu), theItem, theName);
  352.                     OpenDeskAcc(theName);
  353.             }
  354.             break;
  355.         case idFileMenu:
  356.             switch(theItem) {
  357.                 case NewItem:
  358.                     DoNewDocument();
  359.                     PrepareMenus();
  360.                     DrawMenuBar();
  361.                     break;
  362.                 case OpenItem:
  363.                     DoOpenDocument();
  364.                     PrepareMenus();
  365.                     DrawMenuBar();
  366.                     break;
  367.                 case CloseItem:
  368.                     if (theDocument) {
  369.                         CloseDocument(theDocument);
  370.                         PrepareMenus();
  371.                         DrawMenuBar();
  372.                     }
  373.                     break;
  374.                 case SaveItem:
  375.                     if (theDocument)
  376.                         DoSaveDocument(theDocument);
  377.                     break;
  378.                 case SaveAsItem:
  379.                     if (theDocument)
  380.                         DoSaveAsDocument(theDocument);
  381.                     break;
  382.                 case RevertItem:
  383.                     DisableUndoDrag();
  384.                     if (theDocument)
  385.                         DoRevertDocument(theDocument);
  386.                     break;
  387.                 case QuitItem:
  388.                     gQuitting = true;
  389.                     while ((gQuitting) && (theDocument = IsDocumentWindow(FrontWindow()))) {
  390.                         CloseDocument(theDocument);
  391.                     }
  392.                     if (gQuitting)
  393.                         gQuit = true;
  394.                     break;
  395.             }
  396.             break;
  397.         case idEditMenu:
  398.             switch(theItem) {
  399.                 case iUndo:
  400.                     DoUndoDrag();
  401.                     break;
  402.                 case iCut:
  403.                     if (theDocument) {
  404.                         DisableUndoDrag();
  405.                         TEICut(theDocument->theTE);
  406.                     }
  407.                     break;
  408.                 case iCopy:
  409.                     if (theDocument) {
  410.                         TECopy(theDocument->theTE);
  411.                     }
  412.                     break;
  413.                 case iPaste:
  414.                     if (theDocument) {
  415.                         if (!DoSpecialPaste(theDocument)) {
  416.                             DisableUndoDrag();
  417.                             TEIPaste(theDocument->theTE, 0L, 0L);
  418.                         }
  419.                     }
  420.                     break;
  421.                 case iClear:
  422.                     if (theDocument) {
  423.                         DisableUndoDrag();
  424.                         TEDelete(theDocument->theTE);
  425.                     }
  426.                     break;
  427.                 case iSelectAll:
  428.                     if (theDocument)
  429.                         DoSelectAllDocument(theDocument);
  430.                     break;
  431.             }
  432.             break;
  433.         case idFontMenu:
  434.             DisableUndoDrag();
  435.             GetItem(GetMHandle(idFontMenu), theItem, theName);
  436.             GetFNum(theName, &theFontNumber);
  437.             DoFontSelection(theFontNumber);
  438.             break;
  439.         case idSizeMenu:
  440.             DisableUndoDrag();
  441.             DoSizeSelection(ItemToSize(theItem));
  442.             break;
  443.         case idStyleMenu:
  444.             DisableUndoDrag();
  445.             DoStyleSelection(theItem);
  446.             break;
  447.     }
  448.  
  449.     if (theDocument = IsDocumentWindow(FrontWindow()))
  450.         TEGetHiliteRgn(theDocument->hiliteRgn, theDocument->theTE);
  451.  
  452.     HiliteMenu(0);
  453. }
  454.  
  455.